package com.jgy.juc.paper;

import java.util.concurrent.locks.ReentrantLock;

/**
 * @author 姜光远
 * @date 2023/3/8 20:18
 */
public class Paper implements Runnable{

   static Integer pice ;

    static Integer man;


    public Paper(Integer pice, Integer man) {
        Paper.pice = pice;
        Paper.man = man;
    }

    ReentrantLock lock = new ReentrantLock();

    @Override
    public void run() {

        while (true){
            lock.lock();
            try {
                if (man>400){
                    break;
                }else {
                        int i = (int) (Math.random() * 300);
                        //Thread.sleep(1000);
                        if (pice-i>0){
                            System.out.println(Thread.currentThread().getName()+"抢到了"+i+"元");
                            pice = pice-i;
                            man++;
                            continue;
                        }else if (pice==0){
                            System.out.println(Thread.currentThread().getName()+"没抢到");
                            man++;
                            continue;
                        }else if (pice-i<0){
                            System.out.println(Thread.currentThread().getName()+"抢到了"+pice+"元");
                            pice=0;
                            man++;
                            continue;
                        }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }finally {
                lock.unlock();
            }
        }
    }
}
